<template>
    <div class="add_addr_container">
        <HeaderTop :goBack="true" :headTitle="'添加地址'"></HeaderTop>
        <section class="section_list">
            <span class="section_left">联系人</span>
            <section class="section_right">
                <input type="text" class="input_style" placeholder="收货人姓名" v-model="name">
                <div class="choose_sex">
                    <div class="choose_option" @click="chooseSex(1)">
                        <svg class="address_empty_right"  :class="{choosed: sex == 1}">
                            <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#select"></use>
                        </svg>
                        <span>先生</span>
                    </div>
                    <div class="choose_option" @click="chooseSex(2)">
                        <svg class="address_empty_right"  :class="{choosed: sex == 2}">
                            <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#select"></use>
                        </svg>
                        <span>女士</span>
                    </div>
                </div>
            </section>
        </section>
        <section class="section_list">
            <span class="section_left">联系电话</span>
            <section class="section_right">
                <div class="phone_add">
                    <input type="text" name="phone" placeholder="你的手机号" v-model="phone" class="input_style">
                    <img :src="add_img" height="20" width="20" @click="phone_bk = true">
                </div>
                <input v-if="phone_bk" type="text" name="anntherPhoneNumber" placeholder="备选电话" v-model="anntherPhoneNumber" class="input_style phone_bk">
            </section>
        </section>
        <section class="section_list">
            <span class="section_left">送餐地址</span>
            <section class="section_right">
                <router-link to="/confirmOrder/chooseAddress/addAddress/searchAddress" tag="div" class="choose_address">{{searchAddress? searchAddress.name : '小区/写字楼/学校等'}}</router-link>
                <input type="text" name="address_detail" placeholder="详细地址（如门牌号等）" v-model="address_detail" class="input_style">
            </section>
        </section>
        <section class="section_list">
            <span class="section_left">标签</span>
            <section class="section_right">
                <p>
                    <span :class="{select: tag_type===2}" @click="tag_type=2">家</span>
                    <span :class="{select: tag_type===3}" @click="tag_type=3">学校</span>
                    <span :class="{select: tag_type===4}" @click="tag_type=4">公司</span>
                </p>
                <input type="text" name="tag" placeholder="自定义标签" v-model="tag" class="input_style" @input="inputTag">
            </section>
        </section>
        <div class="determine" @click="addAddress">确定</div>

        <transition name="route-slide">
            <router-view></router-view>
        </transition>
    </div>
</template>

<script>
import {mapState,mapActions} from 'vuex'
import {postAddAddress} from '@api/getData'
export default {
    data(){
        return {
            name:'',
            sex:1,
            add_img:require('../../../../assets/imgs/add_phone.png').default,
            tag:'',
            tag_type:1, // 备注类型
            phone:'',
            phone_bk:'',
            anntherPhoneNumber:'',
            address_detail:'',

        }
    },
    computed:{
        ...mapState(['user','geohash','searchAddress']),
    },
    created(){
        
    },
    methods:{
        ...mapActions(['addNewAddress']),
        async addAddress(){
            if(!this.name){
                this.showAlert = true;
                this.alertText = '请输入姓名'
            }else if(!this.phone){
                this.showAlert = true;
                this.alertText = '请输入电话号码'
            }else if(!this.searchAddress){
                this.showAlert = true;
                this.alertText = '请选择地址'
            }else if(!this.address_detail){
                this.showAlert = true;
                this.alertText = '请输入详细地址'
            }
            
            let tag=''
            if (this.tag_type === 2) {
                tag = '家'
            }else if(this.tag_type === 3){
                tag = '学校'
            }else if(this.tag_type === 4){
                tag = '公司'
            }else{
                tag=this.tag;
            }
            let res = await postAddAddress(this.user.user_id, this.searchAddress.name, this.address_detail, this.geohash, this.name, this.phone, this.anntherPhoneNumber, 0, this.sex, tag, this.tag_type);
            //保存成功返沪上一页，否则弹出提示框
            if (res.message) {
                this.showAlert = true;
                this.alertText = res.message;
            }else {
                this.addNewAddress(1);
                this.$router.go(-1);
            }
        },
        chooseSex(s){
            this.sex=s
        },
        inputTag(){
            if(this.tag.length>0){
                this.tag_type=1
            }
        }
    }
}
</script>

<style lang="scss" scoped>
@import '~@styl/mixin.scss';
.add_addr_container{
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: #f2f2f2;
    padding-top: 1.95rem;
    p, span, input{
        font-family: Helvetica Neue,Tahoma,Arial;
    }
    .section_list{
        display: flex;
        padding: 0 .7rem;
        background: #fff;
        border-bottom: 0.012rem solid #f2f2f2;
        .section_left{
            @include sc(0.7rem,#333);
            line-height: 2.5rem;
            flex: 2;
        }
        .section_right{
            flex: 5;
            display: flex;
            flex-direction: column;
            .input_style{
                height: 2.5rem;
                @include sc(0.7rem,#666);
                border-bottom: 0.012rem solid #f2f2f2;
            }
            .choose_sex{
                display: flex;
                height: 2.5rem;
                align-items: center;
                .choose_option{
                    margin-right: 0.5rem;
                    display: flex;
                    align-items: center;
                    span{
                        @include sc(0.7rem,#333);
                    }
                    .address_empty_right{
                        @include wh(0.8rem,0.8rem);
                        fill: #ccc;
                        margin-right: 0.2rem;
                        &.choosed{
                            fill: $green;
                        }
                    }
                }
            }
            .phone_add{
                display: flex;
                align-items: center;
                .input_style {
                    flex: 1;
                }
            }
            .choose_address{
                line-height: 2.5rem;
                @include sc(0.7rem,#999);
                border-bottom: 0.012rem solid #f2f2f2;
            }
            p{
                margin-top: 0.5rem;
                span{
                    @include sc(0.6rem,#666);
                    padding: 0.1rem 0.3rem;
                    border:0.012rem solid #999;
                    margin-right: 0.5rem;
                    &.select{
                        @include sc(0.6rem,#fff);
                        background: $green;
                        border-color: $green;
                    }
                }
            }
        }
    }
    .determine{
        margin: 0 0.6rem;
        margin-top: 0.6rem;
        background: $green;
        border-radius: 0.2rem;
        @include sc(0.7rem,#fff);
        padding: 0.5rem 0;
        text-align: center;
    } 
    
}
.route-slide-enter,
.route-slide-leave-to
{
    transform: translateX(4rem);
    opacity: 0;
}
.route-slide-enter-active,
.route-slide-leave-active{
    transition: all 0.3s ease-in-out;
}
</style>